package office.service.business.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import office.action.business.form.StorageConditionForm;
import office.action.business.form.StorageForm;
import office.action.business.form.SupplierForm;
import office.action.business.wrapper.StorageWrapper;
import office.bo.business.impl.Storage;
import office.domainset.BizDomainProviderSetImpl;
import office.service.business.IStorageService;
import office.service.business.ISupplierService;
import platform.common.bo.Page;
import platform.common.dao.IDomainProvider;
import platform.common.exception.BusinessException;
import platform.common.service.impl.BaseBizServiceImpl;
import platform.util.Util;

public class StorageServiceImpl extends BaseBizServiceImpl implements IStorageService{
	private ISupplierService supplierService;
	public void editStorage(StorageForm form)throws BusinessException{
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			Storage bo = (Storage)domainProviderSetImpl.getStorageDP().newEmptyObj();
			bo =(Storage) form.copyPropertiesToBO(bo);
			if(null==bo.getId() || ("").equals(bo.getId())){
				bo.generatorUUID();
			}
			//验证是否存在重复记录
			bo.store(domainProviderSet);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("编辑库存信息失败!", e);
		}
	}
	
	public Page getPageInfo(StorageConditionForm condition)throws BusinessException{
		Page page = new Page();
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getStorageDP();
			int pageSize = Integer.parseInt(Util.parsePropertiesReturnStr("project", "Storage.pageSize"));
			page.setPageSize(pageSize);
			Map condMap= new HashMap();
			if(null!=condition&&!Util.isNull(condition.getProductName())){
				condMap.put("productName", condition.getProductName());
			}
			List list = domainProviderSetImpl.getDao().findByDynamicHql(provider, "findStorageByIdAndName", condMap,new HashMap());
			if(list.size()%pageSize == 0){
				page.setTotalPage(list.size()/pageSize);
			}else{
				page.setTotalPage(list.size()/pageSize+1);
			}
			
		}catch(Exception e){
			log.error("", e);
			this.handleBusinessServiceException("获得分页信息失败", e);
		}	
		return page;
	}
	
	public List<StorageWrapper> findAllStorage(StorageConditionForm condition,Page page)throws BusinessException{
		List<StorageWrapper> wrapperList = new ArrayList();
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getStorageDP();
			Map condMap= new HashMap();
			if(null!=condition&&!Util.isNull(condition.getProductName())){
				condMap.put("productName", condition.getProductName());
			}
			List list = domainProviderSetImpl.getDao().findByDynamicHql(provider,"findStorageByIdAndName",condMap,new HashMap(),page);
			StorageWrapper wrapper;
			for(int i=0;i<list.size();i++){
				Storage bo = (Storage)list.get(i);
				wrapper = new StorageWrapper();
				if(bo.getSupplierId()!=null){
					SupplierForm supplierForm=supplierService.getById(bo.getSupplierId());
					bo.setSupplierName(supplierForm.getFactoryName());
				}
				wrapper.copyToWrapper(bo);
				wrapperList.add(wrapper);
			}
		}catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("获取库存信息列表失败！", e);
		}
		return wrapperList;
	}

	public void delStorageById(String id)throws BusinessException{
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getStorageDP();
			domainProviderSetImpl.getDao().deleteById(provider, id);
		}catch(Exception e){
			log.error("", e);
			throw this.handleBusinessServiceException("删除库存信息失败！", e);
		}
	}
	
	public StorageForm getById(String id)throws BusinessException{
		StorageForm form = new StorageForm();
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			Storage bo = (Storage)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getStorageDP(), id);
			form.copyPropertiesFromBO(bo);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("根据id获取库存信息失败!", e);
		}
		return form;
	}

	public ISupplierService getSupplierService() {
		return supplierService;
	}

	public void setSupplierService(ISupplierService supplierService) {
		this.supplierService = supplierService;
	}
	
}
